import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable } from 'rxjs';
import {concatMap, tap, switchMap} from 'rxjs/operators';
import {JitsiToken} from 'src/app/model/jitsitoken';
import {AuthService} from './auth.service'
import { environment } from '../../environments/environment';

const httpOptions = {
  headers: new HttpHeaders({ 'Content-Type': 'application/json' })
};

@Injectable({
  providedIn: 'root'
})
export class JitsiService {

  baseUrl = environment.baseUrl;

  constructor(
    private http: HttpClient,
    private auth: AuthService
  ) { }

  getToken(){
    return this.http.get(this.baseUrl + '/api/jwt_jaas_8x8'); // Observable
  }

  getTokenUser(room: String) {
    var token = localStorage.getItem('token')
    var tokenString: string = ""
    if (token != null) {
      tokenString = token
    }
    
    // var headers = {"x-access-token": token}
    const headers = { 'x-access-token': tokenString}

    let body = {
      "room": room
    }
    return this.http.post<JitsiToken>(this.baseUrl + '/api/jwt_jaas_8x8_user', body, {headers})
  }

  getTokenUserSangaTel(room: String) {
    var token = localStorage.getItem('token')
    var tokenString: string = ""
    if (token != null) {
      tokenString = token
    }
    
    // var headers = {"x-access-token": token}
    const headers = { 'x-access-token': tokenString}

    let body = {
      "room": room
    }
    return this.http.post<JitsiToken>(this.baseUrl + '/api/jwt_jitsi_sangatel', body, {headers})
  }
}
